package com.timothy.business.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.timothy.dao.entity.IdeaMenu;
import com.timothy.dao.entity.IdeaRole;
import com.timothy.dao.entity.IdeaRoleMenu;
import com.timothy.dao.service.IIdeaMenuService;
import com.timothy.dao.service.IIdeaRoleMenuService;

@Service
public class MenuDaoService {

	@Autowired
	private IIdeaMenuService menuService;
	@Autowired
	private IIdeaRoleMenuService roleMenuService;

	/**
	 * 获取角色的功能权限集合
	 * @param role
	 * @return
	 */
	public List<IdeaMenu> getMenuByRole(IdeaRole role) {
		Wrapper<IdeaRoleMenu> roleMenuWrapper = new EntityWrapper<IdeaRoleMenu>().eq(IdeaRoleMenu.ROLE_ID,
				role.getRoleId());
		List<IdeaRoleMenu> roleMenuList = roleMenuService.selectList(roleMenuWrapper);
		if (roleMenuList == null || roleMenuList.size() == 0) {
			return new ArrayList<>();
		}
		List<Integer> menuIdList = roleMenuList.stream().map(menu -> menu.getMenuId()).collect(Collectors.toList());
		Wrapper<IdeaMenu> menuWrapper = new EntityWrapper<IdeaMenu>().in(IdeaMenu.MENU_ID, menuIdList);
		List<IdeaMenu> menuList = menuService.selectList(menuWrapper);
		return menuList;
	}

	/**
	 * 获取所有的功能权限集合
	 * @return
	 */
	public List<IdeaMenu> getAllMenu() {
		List<IdeaMenu> menuList = menuService.selectList(null);
		return menuList;
	}

	/**
	 * 根据父菜单ID获取它的子菜单列
	 * @param parentMenuId
	 * @return
	 */
	public List<IdeaMenu> getMenuListByParentId(Integer parentMenuId) {
		Wrapper<IdeaMenu> wrapper = new EntityWrapper<IdeaMenu>().eq(IdeaMenu.PARENT_ID, parentMenuId);
		List<IdeaMenu> subMenuList = menuService.selectList(wrapper);
		return subMenuList;
	}
}
